/**
 * 
 */
package com.wooophone.common.interceptor;

import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.wooophone.common.web.action.BaseAction;
import com.wooophone.common.web.action.PageBar;
import com.wooophone.common.web.action.Pager;

@SuppressWarnings("serial")
public class PaginationInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {

		if (invocation.getAction() instanceof BaseAction) {
			BaseAction action = (BaseAction) invocation.getAction();
			ActionContext context = invocation.getInvocationContext();
			Pager pager = action.getPager();
			HttpServletRequest request = (HttpServletRequest) context
					.get(ServletActionContext.HTTP_REQUEST);

			PageBar pageBar = new PageBar(pager, getWithoutPageNoUrl(request));

			String rootpath = request.getScheme() + "://"
					+ request.getServerName();
			if (request.getServerPort() != 80) {
				rootpath += (":" + request.getServerPort());
			}
			pageBar.setRootPath(rootpath);

			String pageNo = request.getParameter(PageBar.PCN_PAGE_NO);
			try {
				if (context.getParameters().containsKey("page.currPageNo")) {
					context.getParameters().remove("page.currPageNo");
				}
				if (context.getParameters().containsKey("page.pageSize")) {
					context.getParameters().remove("page.pageSize");
				}
				if (context.getParameters().containsKey("page.groupSize")) {
					context.getParameters().remove("page.groupSize");
				}
				if (pageNo != null && !pageNo.equals("")) {
					int _pageNo = Integer.parseInt(pageNo);
					pager.setCurrPageNo(_pageNo);
				}
			} catch (Exception e) {

			} finally {
				request.setAttribute("pageBar", pageBar);
			}
		}

		return invocation.invoke();
	}

	private String getWithoutPageNoUrl(HttpServletRequest request) {
		Object rewriteUrlObj = request.getAttribute("rewriteUrl");
		String withoutPageNoUrl;
		if (rewriteUrlObj != null && !"".equals(rewriteUrlObj.toString().trim())) {
			withoutPageNoUrl = rewriteUrlObj.toString();
		} else {
			withoutPageNoUrl = request.getRequestURI();
		}
		Enumeration<String> paramNames = request.getParameterNames();
		StringBuffer query = new StringBuffer();
		while (paramNames.hasMoreElements()) {
			String param = paramNames.nextElement();
			if (!param.equals(PageBar.PCN_PAGE_NO)) {
				String[] values = request.getParameterValues(param);
				for (int i = 0; i < values.length; i++) {
					try {
						String pp = java.net.URLEncoder.encode(param, "utf-8");
						String vv = java.net.URLEncoder.encode(values[i],
								"utf-8");
						query.append('&');
						query.append(pp);
						query.append('=');
						query.append(vv);
					} catch (Exception e) {
					}
				}
			}
		}
		if (query.length() > 0) {
			query.replace(0, 1, "?");
		}
		withoutPageNoUrl += query.toString();
		return withoutPageNoUrl;
	}

}
